package wang.aweb.childcar.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wang.aweb.childcar.exception.CarException;
import wang.aweb.childcar.exception.CarExceptionEnum;
import wang.aweb.childcar.model.dao.DepositMapper;
import wang.aweb.childcar.model.pojo.Deposit;
import wang.aweb.childcar.model.query.DepositParam;
import wang.aweb.childcar.model.vo.AgentVO;
import wang.aweb.childcar.model.vo.DepositVO;
import wang.aweb.childcar.service.AgentService;
import wang.aweb.childcar.service.DepositService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DepositServiceImpl implements DepositService {
    @Autowired
    private DepositMapper depositMapper;
    @Autowired
    private AgentService agentService;
    @Override
    public PageInfo getList(Integer page, Integer limit, DepositParam depositParam){
        PageHelper.startPage(page,limit);
        List<Deposit> depositList=depositMapper.selectList(depositParam);
        PageInfo pageInfo=new PageInfo<>(depositList);
        List<DepositVO> depositVOList=depositListToDepositVOList(depositList);
        pageInfo.setList(depositVOList);
        return pageInfo;
    }
    public void add(Integer agentId, Integer money){
        Deposit deposit=new Deposit();
        deposit.setAgentId(agentId);
        deposit.setCreateTime(new Date());
        deposit.setMoney(money);
        deposit.setStatus(1);
        int count=depositMapper.insert(deposit);
        if(count==0){
            throw new CarException(CarExceptionEnum.INSERT_FAILED);
        }
    }
    @Override
    public void tuifei(Integer id){
        Deposit deposit=depositMapper.selectByPrimaryKey(id);
        if(deposit!=null){
            deposit.setStatus(2);
            deposit.setUpdateTime(new Date());
            depositMapper.updateByPrimaryKey(deposit);
        }
        else{
            throw new CarException(CarExceptionEnum.UPDATE_FAILED);
        }
    }
    public List<DepositVO> depositListToDepositVOList(List<Deposit> depositList){
        List<DepositVO> depositVOList=new ArrayList<>();
        for (int i = 0; i < depositList.size(); i++) {
            Deposit deposit =  depositList.get(i);
            depositVOList.add(getDepositVO(deposit));
        }
        return depositVOList;
    }
    public DepositVO getDepositVO(Deposit deposit){
        if(deposit==null){
            return null;
        }else{
            DepositVO depositVO=new DepositVO();
            BeanUtils.copyProperties(deposit,depositVO);
            AgentVO agent=agentService.findVOById(deposit.getAgentId());
            depositVO.setAgentInfo(agent);
            depositVO.setCreate_time(deposit.getCreateTime());
            depositVO.setUpdate_time(deposit.getUpdateTime());
            depositVO.setMoney((float)deposit.getMoney()/100);
            return depositVO;
        }

    }
}
